package top.tagao.service_sms.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.tagao.service_sms.service.SmsService;
import top.tagao.service_sms.utils.ConstantPropertiesUtils;
import top.tagao.yygh.vo.msm.MsmVo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/9 14:22
 */
@Service
public class SmsServiceImpl implements SmsService {

    /**
     * @date 手机发送短信,获取验证码
     */
    @Override
    public boolean send(MsmVo msmVo) {
        // 1. 判断手机号是否为空
        if (StringUtils.isEmpty(msmVo.getPhone())) return false;

        // 2. 整合阿里云短信服务  设置相关参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ConstantPropertiesUtils.REGION_Id,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRECT);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", msmVo.getPhone());
        //签名名称
        request.putQueryParameter("SignName", "tagao旅游");
        //模板code
        request.putQueryParameter("TemplateCode", msmVo.getTemplateCode());
        //模板请求参数
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(msmVo.getParam()));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        }catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }





}
